package handlers

import (
	"awesome-devops/applications/backend/modules/requests"
	"awesome-devops/applications/backend/modules/services"
	"awesome-devops/applications/models"
	"awesome-devops/applications/scheduler"
	"awesome-devops/boot"
	"awesome-devops/helper"
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	awesomehttp "gitee.com/jjawesomejj/awesomehttp/system"
	"reflect"
	"strconv"
)

type DeployHandler struct {
	awesomehttp.HttpBaseHandler
}

func (context *DeployHandler) GetModuleName() string {
	return "/deploy/"
}

func (context *DeployHandler) ActionCreate() interface{} {
	projectId := context.HttpContext.PostOne("project_id").(float64)
	commitId := context.HttpContext.PostOne("commit_id").(string)
	deployComment := context.HttpContext.PostOne("deploy_comment").(string)
	service := services.DeployService{}
	return service.CreateDeploy(int(projectId), commitId, deployComment)
}
func (context *DeployHandler) ActionDiff() interface{} {
	deployIdString := context.HttpContext.Get("deploy_id", "").(string)
	deploy := models.Deploy{}
	deployService := services.DeployService{}
	if deployIdString != "" {
		deployIdInt, err := strconv.Atoi(deployIdString)
		if err != nil {
			return err
		}
		err = deploy.RowByID(deployIdInt, &deploy)
		if err != nil {
			return err
		}
	} else {
		projectId := requests.Input(context.HttpContext, "project_id", reflect.Int, "").(int)
		commitId := requests.Input(context.HttpContext, "commit_id", reflect.String).(string)
		lastCommitId, err := deployService.CheckLastCommitId(projectId, commitId)
		deploy.ProjectId = projectId
		deploy.CommitId = commitId
		deploy.LastCommitId = lastCommitId
		if err != nil {
			return err
		}
	}
	service := deployService
	fmt.Println("开始解析")
	diff, err := service.Diff(deploy)
	fmt.Println(err, "err")
	fmt.Println("解析完成")
	if err != nil {
		return err
	}
	var diffRows []map[string]interface{}
	pageRequest := requests.ResolveDefaultPageRequest(context.HttpContext)
	err = commonhelper.JsonDecodeWithType(commonhelper.JsonEncode(diff), &diffRows)

	if err != nil {
		return nil
	}
	return helper.SlicePage(diffRows, pageRequest.Page, pageRequest.PageSize)
}

func (context *DeployHandler) ActionList() interface{} {
	projectId := context.HttpContext.PostOne("project_id", "").(string)
	status := context.HttpContext.PostOne("status", "").(string)
	pageRequest := requests.ResolveDefaultPageRequest(context.HttpContext)
	keywords := context.HttpContext.PostOne("keyword", "").(string)
	service := services.DeployService{}
	return service.List(keywords, projectId, status, pageRequest.Page, pageRequest.PageSize)
}

func (context *DeployHandler) ActionPublish() interface{} {
	deployId := requests.Input(context.HttpContext, "deploy_id", reflect.Float64, float64(3)).(float64)
	err, i := scheduler.GetScheduler().Run(int(deployId))
	if err != nil {
		return err
	}
	return i
}

func (context *DeployHandler) ActionLog() interface{} {
	deployId := requests.Input(context.HttpContext, "id", reflect.Float64, float64(3)).(float64)
	deploy := models.Deploy{}
	error := boot.GetDbConnection().Where("id", deployId).Table(deploy.TableName()).First(&deploy)
	if error.Error != nil {
		return error.Error
	}
	return deploy
}

func (context *DeployHandler) ActionDeployConfig() interface{} {
	projectId := requests.Input(context.HttpContext, "project_id", reflect.Int).(int)
	service := services.PublishService{}
	return service.DeployConfig(projectId)
}
